<script setup lang="ts">
import { ref } from 'vue'
import { useModifyCompany } from '../composables/useModifyCompany'
import { baseUrl } from '../config'

const props = defineProps<{ companyId: number; name: string; comment: string }>()
const emit = defineEmits(['close'])

const { modifyCompany, loading: saving, error: saveError, result } = useModifyCompany(baseUrl)

const name = ref(props.name)
const comment = ref(props.comment)

async function handleSubmit() {
  await modifyCompany({
    id: props.companyId,
    name: name.value,
    comment: comment.value
  })
  if (result.value && result.value.success) {
    emit('close')
  }
}
</script>

<template>
  <div class="modify-company-panel">
    <h3>编辑组织</h3>
    <form @submit.prevent="handleSubmit">
      <div class="form-item">
        <label>组织名称：</label>
        <input v-model="name" required />
      </div>
      <div class="form-item">
        <label>组织说明：</label>
        <input v-model="comment" />
      </div>
      <button type="submit" :disabled="saving">保存</button>
    </form>
    <div v-if="saveError" class="error">{{ saveError }}</div>
  </div>
</template>

<style scoped>
.modify-company-panel {
  max-width: 400px;
  margin: 0 auto;
  padding: 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}
.form-item {
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
}
label {
  margin-bottom: 4px;
  font-weight: bold;
}
input {
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #ddd;
}
button {
  padding: 8px 24px;
  border: none;
  border-radius: 6px;
  background: #1890ff;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}
button:disabled {
  background: #bcdffb;
  cursor: not-allowed;
}
.error {
  color: #ff4d4f;
  margin-top: 8px;
}
</style>